package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加收货地址
     * @param addressBook
     * @param session
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook, HttpSession session) {
        addressBook.setUserId((Long) session.getAttribute("user"));
        if (addressBookService.save(addressBook)) {
            return R.success("收货地址添加成功");
        } else {
            return R.error("收货地址添加失败");
        }
    }

    /**
     * 查询所有收货地址
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 查询选中收货地址的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getId, id);
        AddressBook addressBook = addressBookService.getOne(lambdaQueryWrapper);
        return R.success(addressBook);
    }

    /**
     * 修改收货地址信息
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
//        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(AddressBook::getId, addressBook.getId());
        if (addressBookService.updateById(addressBook)) {
            return R.success("收货地址修改成功");
        } else {
            return R.error("收货地址修改失败");
        }
    }

    /**
     * 设置默认地址
     * @param id
     * @return
     */
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody Long id){
        AddressBook addressBook = addressBookService.getById(id);
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, addressBook.getUserId());
        List<AddressBook> addressBooks = addressBookService.list(lambdaQueryWrapper);
        for (AddressBook book : addressBooks) {
            book.setIsDefault(0);
            addressBookService.updateById(book);
        }
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设为默认地址成功");
    }

    @GetMapping("/default")
    public R<AddressBook> getDefault(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, userId).eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookService.getOne(lambdaQueryWrapper);
        if (null == addressBook){
            return R.error("没有找到该对象");
        }else {
            return R.success(addressBook);
        }
    }
}
